package com.service.system.user.impl;

import com.dao.*;
import com.entity.system.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.service.system.user.UserInfoService;
import com.util.Const;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class UserInfoServiceImpl implements UserInfoService{

    @Autowired
    UserInfoMapper userInfoMapper;

    @Autowired
    StaticUserRoleMapper staticUserRoleMapper;

    @Autowired
    StaticRolePermissionMapper staticRolePermissionMapper;

    @Autowired
    StaticPermissionMapper staticPermissionMapper;

    @Autowired
    StaticRoleMapper staticRoleMapper;

    @Autowired
    AdvertMapper advertMapper;

    @Autowired
    OrderPayMapper orderPayMapper;

    @Autowired
    HatProvinceMapper hatProvinceMapper;

    @Autowired
    HatCityMapper hatCityMapper;

    @Autowired
    HatAreaMapper hatAreaMapper;

    @Override
    public UserInfo findUserByPhoneAndPwd(Map<String, String> map) {
        return userInfoMapper.findUserByPhoneAndPwd(map);
    }

    @Override
    public Map<String, Object> getMenu(Long userId) {
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("userId",userId);
        map.put("differentiate",1);
        StaticUserRole staticUserRole = staticUserRoleMapper.findStaticUserRoleByMap(map);
        map.clear();
        List<Long> permissionIds = staticRolePermissionMapper.findPermissionIdByRoleId(staticUserRole.getRoleId());
        StaticRole staticRole = staticRoleMapper.selectByPrimaryKey(staticUserRole.getRoleId());
//        System.out.println(permissionIds.toString());
//        String ids = StringUtils.strip(permissionIds.toString(),"[]");
        if(permissionIds.size() <= 0){
            map.put("code",1);
            map.put("message","查询成功,暂无权限");
            return map;
        }
        map.put("permissionIds",permissionIds);
        map.put("permissionType",1);
        map.put("permissionParent",0);
        List<StaticPermission> permissionList = staticPermissionMapper.findStaticPermissionByIdAndType(map);
        map.clear();
        if(permissionList.size() <= 0){
            map.put("code",1);
            map.put("message","查询成功,暂无菜单");
            return map;
        }

        for (StaticPermission sp : permissionList) {
            map.put("permissionParent",sp.getId());
            map.put("permissionIds",permissionIds);
            List<StaticPermission> permissionList1 = staticPermissionMapper.findStaticPermissionByIdAndType(map);
            for(StaticPermission ss : permissionList1){
                //是否有增删改查权限
                if(StringUtils.isNotEmpty(staticRole.getAddPermission())){
                    if(new BigInteger(staticRole.getAddPermission()+"").testBit(Integer.parseInt(ss.getId()+""))){
                        ss.setAdd(1);
                    }
                }
                if(StringUtils.isNotEmpty(staticRole.getDelPermission())){
                    if(new BigInteger(staticRole.getDelPermission()+"").testBit(Integer.parseInt(ss.getId()+""))){
                        ss.setDel(1);
                    }
                }
                if(StringUtils.isNotEmpty(staticRole.getChangePermission())){
                    if(new BigInteger(staticRole.getChangePermission()+"").testBit(Integer.parseInt(ss.getId()+""))){
                        ss.setEdit(1);
                    }
                }
                if(StringUtils.isNotEmpty(staticRole.getQueryPermission())){
                    if(new BigInteger(staticRole.getQueryPermission()+"").testBit(Integer.parseInt(ss.getId()+""))){
                        ss.setQuery(1);
                    }
                }
            }
            sp.setList(permissionList1);
        }
        map.clear();
        map.put("code",1);
        map.put("menus",permissionList);
        map.put("staticRole",staticRole);
        map.put("message","查询成功");
        return map;
    }

    @Override
    public Map<String, Object> fundUserList(Map<String, String> map) {
        Map<String,Object> map1 = new HashMap<>();
        int pageSize = 10;
        int pageNum = 1;
        if(StringUtils.isNotEmpty(map.get("pageSize"))){
            pageSize = Integer.parseInt(map.get("pageSize"));
        }
        if(StringUtils.isNotEmpty(map.get("pageNum"))){
            pageNum = Integer.parseInt(map.get("pageNum"));
        }
        PageHelper.startPage(pageNum,pageSize);
        List<UserInfo> userInfos = userInfoMapper.fundUserList(map);
        PageInfo<UserInfo> pageInfo = new PageInfo<>(userInfos);
        map1.put("code", Const.SUCCESS_CODE);
        map1.put("message","查询成功");
        map1.put("list",pageInfo.getList());
        map1.put("page",pageInfo);
        return map1;
    }

    @Override
    public Map<String, Object> findUserByPhone(String phone) {
        Map<String,Object> map = new HashMap<>();
        UserInfo userInfo = userInfoMapper.findUserByPhone(phone);
        if(userInfo != null){
            map.put("code",Const.SUCCESS_CODE);
            map.put("user",userInfo);
            map.put("message","查询成功");
        }else {
            map.put("code",Const.ERROR_CODE);
            map.put("message","查无数据");
        }
        return map;
    }

    @Override
    public Map<String, Object> statistics(Map<String,Object> map1) {
        Map<String,Object> map = new HashMap<>();
        AgentNew agentNew = (AgentNew) map1.get("an");
        if(null != agentNew){

            if(agentNew.getAgentLevel() == 1){
//                String area = hatAreaMapper.findByAreaId(agentNew.getCountyCode()).getArea();
//                String city = hatCityMapper.findByCityId(agentNew.getCityCode()).getCity();
//                String province = hatProvinceMapper.findByProvinceId(agentNew.getProvinceCode()).getProvince();
//                map.put("area",area.substring(0,area.length()-1)+"%");
//                map.put("city",city.substring(0,city.length()-1)+"%");
//                map.put("province",province.substring(0,province.length()-1)+"%");
                map.put("area",agentNew.getCountyCode());
                map.put("city",agentNew.getCityCode());
                map.put("province",agentNew.getProvinceCode());
            }else if(agentNew.getAgentLevel() == 2){
//                String city = hatCityMapper.findByCityId(agentNew.getCityCode()).getCity();
//                String province = hatProvinceMapper.findByProvinceId(agentNew.getProvinceCode()).getProvince();
//                map.put("city",city.substring(0,city.length()-1)+"%");
//                map.put("province",province.substring(0,province.length()-1)+"%");
                map.put("city",agentNew.getCityCode());
                map.put("province",agentNew.getProvinceCode());
            }else if(agentNew.getAgentLevel() == 3){
//                String province = hatProvinceMapper.findByProvinceId(agentNew.getProvinceCode()).getProvince();
//                map.put("province",province.substring(0,province.length()-1)+"%");
                map.put("province",agentNew.getProvinceCode());
            }
        }

        Long sumRedEnvelopeAmount = advertMapper.sumRedEnvelopeAmount(map);
        map.put("payState",2);
        Long sumPayPrice = orderPayMapper.sumPayPrice(map);
//        map.clear();
//        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        int money = calendar.get(Calendar.MONTH)+1;
        List<Double> sumPayPrices = new ArrayList<>();
        List<Double> sumRedEnvelopeAmounts = new ArrayList<>();
        for(int i = 1;i <= money;i++){
            map.put("time","2018-0"+i+"%");
            Long advSum = advertMapper.sumRedEnvelopeAmount(map);
            if(null == advSum){
                advSum = 0L;
            }
            sumRedEnvelopeAmounts.add(Double.parseDouble(advSum.toString()) / 100);

            Long paySum = orderPayMapper.sumPayPrice(map);
            if(null == paySum){
                paySum = 0L;
            }
            sumPayPrices.add(Double.parseDouble(paySum.toString()) / 100);
        }

        map.put("sumRedEnvelopeAmount",sumRedEnvelopeAmount);
        map.put("sumPayPrice",sumPayPrice);
        map.put("sumPayPrices",sumPayPrices);
        map.put("sumRedEnvelopeAmounts",sumRedEnvelopeAmounts);
        map.put("code",Const.SUCCESS_CODE);
        return map;
    }
}
